import { message } from 'antd';

import api from '../../api';

export default {
  namespace: 'tradeManageM',
  state: {
    columns: [
      {
        title: '产品名称',
        dataIndex: 'productName',
        key: 'productName',
        render: (val, record) => {
          if (val) {
            return <span>{`${record.productCode}-${val}`}</span>;
          }
        },
      },
      {
        title: '交易笔数',
        dataIndex: 'transactionsNumber',
        key: 'transactionsNumber',
        type: 'number',
        sorter: (a, b) => a.transactionsNumber - b.transactionsNumber,
      },
      {
        title: '交易金额',
        dataIndex: 'transactionsAmount',
        key: 'transactionsAmount',
        type: 'money',
        sorter: (a, b) => a.transactionsAmount - b.transactionsAmount,
      },
      {
        title: '主要资产类型',
        dataIndex: 'assetType',
        key: 'assetType',
        render: (val) => val ?? '--',
      },
      {
        // 主要交易市场（排序） 未知排序规则
        title: '主要交易市场',
        dataIndex: 'exchangeCode',
        key: 'exchangeCode',
        render: (val) => val ?? '--',
      },
    ],
    instructionFlag: false,
    bookingFlag: false,
  },
  effects: {
    // 获取交易管理统计信息
    *getTotalData({ payload, callback }, { call }) {
      try {
        const response = yield call(api.tradeManage.getTotalData, payload);
        // const response = { code: 0, data: { transactionsNumber:3,transactionsAmount:333,instructionNumber:4, instructionAmount:444,bookingNumber:5,bookingAmount:555} }
        if (response?.success) {
          callback(response.data);
        } else {
          message.error(response.msg || '获取统计信息失败');
        }
      } catch (e) {
        console.log(e);
      }
    },

    // 获取表格信息
    *getTableData({ payload, callback }, { call }) {
      try {
        const response = yield call(api.tradeManage.getTableData, payload);
        if (response?.code == 0) {
          // 倒序
          const reverseOrderData = response.data.userViewtradetradeCountVoL.sort((a, b) => b?.transactionsNumber - a?.transactionsNumber);
          // 筛选数据交易笔数为0删除数据
          let data = [];
          if (reverseOrderData.length > 0) {
            data = [...reverseOrderData].filter((v) => v.transactionsNumber !== 0);
          }
          callback(data, response.data);
        } else {
          message.error(response.msg || '获取表格信息失败');
        }
      } catch (e) {
        console.log(e);
      }
    },
  },

  reducers: {
    setState(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
  },
};
